package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PropertyContract;
import com.ruoyi.system.mapper.PropertyContractMapper;
import com.ruoyi.system.service.IPropertyContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class IPropertyContractServiceImpl implements IPropertyContractService {

    @Autowired
    private PropertyContractMapper propertyContractMapper;

    @Override
    public List<PropertyContract> selectPropertyContractList(PropertyContract propertyContract) {
        return propertyContractMapper.selectPropertyContractList(propertyContract);
    }

    @Override
    public PropertyContract getPropertyContractById(String contractId) {
        return propertyContractMapper.getPropertyContractById(contractId);
    }

    @Override
    public int insertPropertyContract(PropertyContract propertyContract) {
        String contractId = UUID.randomUUID().toString().replaceAll("-","");
        propertyContract.setContractId(contractId);
        return propertyContractMapper.insertPropertyContract(propertyContract);
    }

    @Override
    public int updatePropertyContract(PropertyContract propertyContract) {
        return propertyContractMapper.updatePropertyContract(propertyContract);
    }

    @Override
    public int removePropertyContract(String ids) {
        return propertyContractMapper.removePropertyContract(Convert.toStrArray(ids));
    }

    @Override
    public String importPropertyContract(List<PropertyContract> pcList) {
        //校验 状态 类型 日期
        int errorData = 0;
        int updateCount = 0;
        int insertCount = 0;
        StringBuilder errorDatas = new StringBuilder();
        for(PropertyContract pc:pcList){
            if(null==pc.getNum()){
                errorData++;
                String errStr = "合同" + pc.getContractNo() + "序号为空；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(!"1".equals(pc.getStatus())&&!"2".equals(pc.getStatus())){
                errorData++;
                String errStr = "合同" + pc.getContractNo() + "状态异常；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(!"1".equals(pc.getContractType())&&!"2".equals(pc.getContractType())){
                errorData++;
                String errStr = "合同" + pc.getContractNo() + "类型错误；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(StringUtils.isNotEmpty(pc.getContractId())){  //update
                try{
                    PropertyContract propertyContract = propertyContractMapper.getPropertyContractById(pc.getContractId());
                    if(propertyContract!=null){
                        propertyContractMapper.updatePropertyContract(pc);
                    }else{
                        propertyContractMapper.insertPropertyContract(pc);
                    }
                    updateCount++;
                }catch (Exception e){
                    e.printStackTrace();
                    errorData++;
                    String errStr = "合同" + pc.getContractNo() + "修改失败；"+e.getMessage()+"</br>";
                    errorDatas.append(errStr);
                    continue;
                }
            }else{  //insert
                try{
                    pc.setContractId(UUID.randomUUID().toString().replaceAll("-",""));
                    propertyContractMapper.insertPropertyContract(pc);
                    insertCount++;
                }catch (Exception e){
                    e.printStackTrace();
                    errorData++;
                    String errStr = "合同" + pc.getContractNo() + "新增失败；"+e.getMessage()+"</br>";
                    errorDatas.append(errStr);
                    continue;
                }
            }
        }
        String message;
        if(pcList.size()>0){
            message = "共导入"+pcList.size()+"条数据。</br>";
        }else{
            return "无数据";
        }
        message = message + "成功新增" + insertCount + "条数据，成功更新"+updateCount+"条数据</br>";
        if(StringUtils.isNotEmpty(errorDatas.toString())){
            message = message + "其中" + errorData + "条数据导入失败，错误原因如下：</br>";
            message = message + errorDatas.toString();
        }
        return message;
    }
}
